<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
		<link href="css/bootstrap.min.css" rel="stylesheet">
		<link href="css/materialdesignicons.min.css" rel="stylesheet">
		<!--标签插件-->
		<link rel="stylesheet" href="js/jquery-tags-input/jquery.tagsinput.min.css">
		<link href="css/style.min.css" rel="stylesheet">

		<link href="css/animate.css" rel="stylesheet">
	</head>

	<body>
		<div class="container-fluid p-t-15">

			<div class="row">
				<div class="col-lg-12">
					<div class="card">
						<div class="card-body">

							<form action="#!" method="post" class="row">
								<!-- <div class="col-md-5 col-md-offset-3"> -->
								<div class="col-md-5">
									<div class="form-group col-md-12">
										<label for="userId">员工ID</label>
										<input type="text" class="form-control" id="userId" name="userId" value=""
											   placeholder="请输入员工ID" readonly />
									</div>
									<div class="form-group col-md-12">
										<label for="userName">员工姓名</label>
										<input type="text" class="form-control" id="userName" name="userName" value=""
											placeholder="请输入员工名称" />
									</div>
									<div class="form-group col-md-12">
										<label for="userType">员工角色</label>
										<select id="userType" class="form-control">
											<option value="0">管理员</option>
											<option value="1">销售员</option>
										</select>
									</div>
									<div class="form-group col-md-12">
										<label for="remark">简历</label>
										<textarea class="form-control" id="remark" name="remark" rows="5" value=""
											placeholder="请输入员工简历"></textarea>
									</div>
									<div class="form-group col-md-12">
										<button onclick="modUser()" type="button" class="btn btn-primary ajax-post"
											target-form="add-form">修改员工</button>
									</div>
								</div>
								
							</form>

						</div>
					</div>
				</div>

			</div>

		</div>

		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script type="text/javascript" src="js/bootstrap.min.js"></script>
		<!--标签插件-->
		<script src="js/jquery-tags-input/jquery.tagsinput.min.js"></script>
		<script type="text/javascript" src="js/main.min.js">

			<!--消息提示-->
			<script src="js/bootstrap-notify.min.js"></script>
		<script type="text/javascript" src="js/lightyear.js"></script>
		<script>

			$(function(){
				//加载要修改的员工信息
				loadModUser();
			});

			/**
			 * 获取sessionStorage中要修改的员工id，根据id加载要修改的员工信息
			 */
			function loadModUser(){
				let userId = sessionStorage.getItem("modUserId");
				if(!userId){
					lightyear.notify("查无要修改的员工", "danger", 1000);
					location.href = "users_list.html";
					return;
				}
				//从数据库，根据ID查询商品
				$.getJSON("users/queryById/"+userId, function(rvo){
					console.log("users/queryById/: ", rvo);
					if(rvo.code != 1){
						lightyear.notify(rvo.msg, "danger", 1000);
						location.href = "pro_list.html";
						return;
					}

					// 显示要修改的对象到页面
					showModInfo(rvo.content);

				})

			}

			/**
			 * 显示要修改的对象到页面
			 */
			function showModInfo(modUser){
				$("#userId").val(modUser.userId);
				$("#userName").val(modUser.userName);
				$("#userType").val(modUser.userType);
				$("#remark").val(modUser.remark);
			}


			function modUser(){
				//1- 获取添加商品的信息
				let userId = $("#userId").val();
				let userName = $("#userName").val();
				let userType = $("#userType").val();
				let remark = $("#remark").val();
				//2- 封装提交参数
				let param = {
					"userId": userId,
					"userName": userName,
					"userType": userType,
					"remark": remark
				}

				//3- 请求添加
				$.getJSON("users/edit", param, function(rvo){
					console.log("users/edit", rvo);
					//添加失败
					if(rvo.code != 1){
						lightyear.notify(rvo.msg, "danger", 1000);
						return;
					}

					//添加成功，跳转到页面显示
					location.href = "./users_list.html";

				});

			}



		</script>
	</body>
</html>
